
//Utility to mimic standard OOP behaviour... JavaScript behaves a little different usually because of its architecture, especially concerning heritage.
//This script was copied from http://www.kevlindev.com/tutorials/javascript/inheritance/ and his copyright terms apply.


function utilityJS(){}

utilityJS.prototype.extend = function(subClass, baseClass) {
	function inheritance() {}
	inheritance.prototype = baseClass.prototype;
	
	subClass.prototype = new inheritance();
	subClass.prototype.constructor = subClass;
	subClass.baseConstructor = baseClass;
	subClass.superClass = baseClass.prototype;
}

//Javascript code to write/read files in Firefox, copyright Andreas Halter
utilityJS.prototype.fileRead = function(str_Filename){ 
        try{
           var obj_File = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
           obj_File.initWithPath(str_Filename);
 
           var obj_InputStream =  Components.classes["@mozilla.org/network/file-input-stream;1"].createInstance(Components.interfaces.nsIFileInputStream);
           obj_InputStream.init(obj_File,0x01,0444,null);
 
           var obj_ScriptableIO = Components.classes["@mozilla.org/scriptableinputstream;1"].createInstance(Components.interfaces.nsIScriptableInputStream);
           obj_ScriptableIO.init(obj_InputStream);
        } catch (e) { alert(e); }
 
        try {
                var str = obj_ScriptableIO.read(obj_File.fileSize-1);
        } catch (e) { dump(e); }
        obj_ScriptableIO.close();
        obj_InputStream.close();
        return str;
}

utilityJS.prototype.fileWrite = function(str_Buffer,str_Filename){
    try{
        var obj_File = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
        obj_File.initWithPath(str_Filename);
        if(!obj_File.exists()) obj_File.create(0x00,0644);
    } catch (e) { alert(e); }
    try {
        var obj_Transport = Components.classes["@mozilla.org/network/file-output-stream;1"].createInstance(Components.interfaces.nsIFileOutputStream);
        obj_Transport.init( obj_File, 0x04 | 0x08 | 0x10, 064, 0 );
        obj_Transport.write(str_Buffer,str_Buffer.length);
        obj_Transport.close();
    } catch (e) {
        alert(e);
    }
}

utilityJS.prototype.fileSelect = function(mode,title){
   var nsIFilePicker = Components.interfaces.nsIFilePicker;
   var fp = Components.classes["@mozilla.org/filepicker;1"].createInstance(nsIFilePicker);
   fp.init(window, title, nsIFilePicker[mode]);
   var res=fp.show();
   if (res==nsIFilePicker.returnOK) return fp.file;
   else return undefined;
}

//From this point on, everything is (c) 2006 Hans Schmucker

//A simple extension to the Array class, so that we can swap two elements:
Array.prototype.swap=function(a, b){
	var tmp=this[a];
	this[a]=this[b];
	this[b]=tmp;
}

//A simple extension to the Array class, so that we can move an element:
Array.prototype.move=function(src, dest){
	var tmp=this[src];
	this.splice(src,1);
	this.splice(dest,0,tmp);
}
